//@author A0121492M
public class DoneCommand extends Command {
	public int taskId = Task.EMPTY_ID;
	public boolean isDone = true;
	public String taskTitle = Task.EMPTY_STRING;
	
	public ExecutionReply execute()
			throws TaskNotFoundException, NotUniqueException {
		assert(this.taskId != Task.EMPTY_ID ^ this.taskTitle != Task.EMPTY_STRING);
		int index = -1;
		if (this.taskId != Task.EMPTY_ID) {
			Task doneTask = new Task();
			doneTask.id = this.taskId;
			index = Logic.loadedData.indexOf(doneTask);
		} else if (this.taskTitle != Task.EMPTY_STRING) {
			index = Logic.findIndexByTitle(this.taskTitle);
		}
		
		Task task = new Task();
		try {
			task = Logic.loadedData.get(index);
			this.taskId = task.id;
			task.isDone = this.isDone;
		} catch (IndexOutOfBoundsException e) {
			throw new TaskNotFoundException();
		}
		
		SingleTaskReply r = new SingleTaskReply();
		r.task = task;
		Logic.saveData();
		Logic.updateCommandHistory(this);
		return r;
	}
	
	public void undo() {
		assert(this.taskId != Task.EMPTY_ID);
		Task dummy = new Task();
		dummy.id = this.taskId;
		int index = Logic.loadedData.indexOf(dummy);
		Task task = Logic.loadedData.get(index);
		task.isDone = !this.isDone;
		Logic.loadedData.set(index, task);
		Logic.saveData();
	}
	
	public void redo() {
		assert(this.taskId != Task.EMPTY_ID);
		Task dummy = new Task();
		dummy.id = this.taskId;
		int index = Logic.loadedData.indexOf(dummy);
		Task task = Logic.loadedData.get(index);
		task.isDone = this.isDone;
		Logic.loadedData.set(index, task);
		Logic.saveData();
	}
}
